package com.toolsder.demo.spring.security.jwt.config.handler;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.toolsder.demo.spring.security.jwt.entity.JsonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * created by 猴子请来的逗比 On 2020/8/27
 *
 * @author by 猴子请来的逗比
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException {
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.getWriter().print(gson.toJson(JsonResult.forbidden()));
    }
}
